//
//  UserViewController.m
//  逗她盒子
//
//  Created by Qiaofeng on 15/6/15.
//  Copyright (c) 2015年 Qiaofeng. All rights reserved.
//

#import "UserViewController.h"
#import "AFNetworking.h"
#import "SearchViewController.h"
#import "PlayerInfoModel.h"
#import "UIImageView+WebCache.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@interface UserViewController ()
@property (nonatomic, strong)UIScrollView *scrollView;
@property (nonatomic, strong)UIImageView *headPortrait;
@property (nonatomic, strong)UILabel *personaname;
@property (nonatomic, strong)UITableView *comprehensive;
@property (nonatomic, strong)UITableView *record;
@property (nonatomic, strong)UITableView *teammatesOrOpponents;
@property (nonatomic, strong)UITableView *trend;
@end

@implementation UserViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
- (void)json
{
    [self userPlayerInfoArrayJson];
    [self userHeroUsedInfoJson];
}

- (void)userPlayerInfoArrayJson
{
    self.userPlayerInfoArray = [[NSMutableArray alloc]init];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *url = [NSString stringWithFormat:@"http://api.maxjia.com/api/player/info/?"];
    [manager.requestSerializer setValue:@"http://api.maxjia.com/" forHTTPHeaderField:@"Referer"];
    NSDictionary *parameters = @{@"steam_id":self.steam_id, @"phone_num":@"00000000000", @"pkey":@"randpkey", @"os_type":@"iOS",@"os_version":@"8.3",@"time":@"1433927795", @"version":@"1.0.0"};
    [manager GET:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *BigDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *resultDic = [BigDic objectForKey:@"result"];
        self.playerInfoModel = [[PlayerInfoModel alloc]init];
        [self.playerInfoModel setValuesForKeysWithDictionary:resultDic];
        NSURL *url = [NSURL URLWithString:self.playerInfoModel.avatar_url_full];
        [self.headPortrait sd_setImageWithURL:url];
        self.personaname.text = self.playerInfoModel.personaname;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];

}

- (void)userHeroUsedInfoJson
{
    self.userHeroUsedInfoArray = [[NSMutableArray alloc]init];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *url = [NSString stringWithFormat:@"http://api.maxjia.com/api/player/summary/?"];
    [manager.requestSerializer setValue:@"http://api.maxjia.com/" forHTTPHeaderField:@"Referer"];
    NSDictionary *parameters = @{@"steam_id":self.steam_id, @"phone_num":@"00000000000", @"pkey":@"randpkey", @"os_type":@"iOS",@"os_version":@"8.3",@"time":@"1433927795", @"version":@"1.0.0"};
    [manager GET:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *BigDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *resultDic = [BigDic objectForKey:@"result"];
        NSArray *hero_used_infoDic = [resultDic objectForKey:@"hero_used_info"];
        for (NSDictionary *hero_used_info in hero_used_infoDic) {

        }

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

- (void)createScrollView
{
   self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 150, WIDTH, HEIGHT - 150)];
    self.scrollView.backgroundColor = [UIColor whiteColor];
    
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 4, 0);
    [self.scrollView setPagingEnabled:YES];
    [self.view addSubview:self.scrollView];
    
    self.comprehensive = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 150)];
    [self.comprehensive setBackgroundColor:[UIColor blueColor]];
    [self.scrollView addSubview:self.comprehensive];
    
    self.record = [[UITableView alloc]initWithFrame:CGRectMake(WIDTH , 0, WIDTH,HEIGHT - 150)];
    [self.record setBackgroundColor:[UIColor yellowColor]];
    [self.scrollView addSubview:self.record];
    
    self.teammatesOrOpponents = [[UITableView alloc]initWithFrame:CGRectMake(WIDTH * 2, 0, WIDTH,HEIGHT - 150)];
    [self.teammatesOrOpponents setBackgroundColor:[UIColor greenColor]];
    [self.scrollView addSubview:self.teammatesOrOpponents];
    
    self.trend = [[UITableView alloc]initWithFrame:CGRectMake(WIDTH * 3, 0, WIDTH,HEIGHT - 150)];
    [self.trend setBackgroundColor:[UIColor redColor]];
    [self.scrollView addSubview:self.trend];
     self.automaticallyAdjustsScrollViewInsets = NO;    // 关闭自适应 cell上面不会留白
}




- (void)createHeadView
{
    
    UIImageView *headImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"barImage.png"]];
    headImageView.frame = CGRectMake(0, 0, WIDTH, 150);
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 150)];
    [headView addSubview:headImageView];
    [self.view addSubview:headView];

    UIImageView *back = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"iconfont-back.png"]];
    [back setFrame:CGRectMake(5, 30, 35, 30)];
    [self.view addSubview:back];
    back.userInteractionEnabled = YES;
    UITapGestureRecognizer *singleTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backAction:)];
    [back addGestureRecognizer:singleTap1];
    
    // 头像
    self.headPortrait = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH / 2 * 0.88 , 30, 50, 50)];
    self.headPortrait.layer.cornerRadius = 25;
    self.headPortrait.clipsToBounds = YES;
    [self.view addSubview:self.headPortrait];
    
    // 头像下方名字
    self.personaname = [[UILabel alloc]initWithFrame:CGRectMake(0, 85, WIDTH, 25)];
    self.personaname.textColor = [UIColor whiteColor];
    self.personaname.font = [UIFont systemFontOfSize:16];
    self.personaname.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.personaname];
    


}
- (void)backAction:(id)sender
{
    [self.navigationController setNavigationBarHidden:NO];
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self json];
    [self createScrollView];
    [self createHeadView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
